package samsung.sds.systemParamManagement.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import samsung.sds.system.common.baseQuery.model.PageData;
import samsung.sds.system.common.model.QueryParam;
import samsung.sds.system.utils.CommonTools;
import samsung.sds.systemParamManagement.model.SystemParamEntity;
import samsung.sds.systemParamManagement.service.SystemParamManagementService;

/**
 * @author c1.wang
 * @createDate 2016-02-22
 * 
 * 系统参数信息控制类
 */
@Controller
@RequestMapping("/systemParamManagement")
public class SystemParamManagementController {
	@Autowired
	private SystemParamManagementService systemParamManagementServiceImpl;

	/**
	 * 跳转systemParamList页面
	 * 
	 * @param request
	 * @param modelMap
	 * @return
	 */
	@RequestMapping(value = "/systemParamList", method = RequestMethod.POST)
	public String findSystemParamList(HttpServletRequest request,
			ModelMap modelMap) {
		String pageType = request.getParameter("pageType");
		modelMap.addAttribute("pageType", pageType);
		return "/systemParamManagement/systemParamList";
	}

	/**
	 * 实现系统参数的分页查询
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/listPageData", method = RequestMethod.POST)
	@ResponseBody
	public PageData<SystemParamEntity> listPageData(
			HttpServletRequest request) {
		QueryParam param = CommonTools.getQueryParam(request);
		return systemParamManagementServiceImpl.queryListPage(param);
	}

	/**
	 * 新增或修改系统参数
	 * 
	 * @param request
	 * @param systemParam
	 * @return
	 */
	@RequestMapping(value = "/addOrUpdateSystemParam", method = RequestMethod.POST)
	@ResponseBody
	public String addOrUpdateSystemParam(HttpServletRequest request,
			SystemParamEntity systemParam) {
		String opType = request.getParameter("optype");
		return systemParamManagementServiceImpl.addOrUpdateSystemParam(opType, systemParam);		
	}

	/**
	 * 加载完addOrUpdateUser.jsp后，执行请求，将数据写入页面。
	 * 
	 * @param paramKey
	 * @param modelMap
	 * @return
	 */
	@RequestMapping(value = "/creatSystemParam", method = RequestMethod.POST)
	public String creatSystemParam(String paramKey, ModelMap modelMap) {
		SystemParamEntity systemParam = systemParamManagementServiceImpl
				.findEntityById(paramKey);
		modelMap.addAttribute("systemParam", systemParam);
		return "/systemParamManagement/addOrUpdateSystemParam";
	}

	/**
	 * 删除系统参数
	 * 
	 * @param paramKey
	 * @return
	 */
	@RequestMapping(value = "/deleteSystemParam", method = RequestMethod.POST)
	@ResponseBody
	public String deleteSystemParam(String paramKey) {
		return systemParamManagementServiceImpl.deleteSystemParam(paramKey);
	}

	/**
	 * 检查选项编号是否重复
	 * 
	 * @param paramKey
	 * @return
	 */
	@RequestMapping(value = "/checkParamKey", method = RequestMethod.POST)
	@ResponseBody
	public boolean checkParamKey(String paramKey) {
		return systemParamManagementServiceImpl.checkParamKeyRepeat(paramKey);
	}

}
